package com.bibrApp.difu.fragment.send_mvp;

import android.content.Context;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.bibrApp.difu.R;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class MySendAdapter extends RecyclerView.Adapter<SuperViewHolder> {
    List<SendBean> list = new ArrayList<>();
    Context context;

    private OnItemClickLitener mOnItemClickLitener;

    public interface OnItemClickLitener {
        void onItemOnPlusClick(View view, int position);

        void onItemOnReduceClick(View view, int position);

    }

    public MySendAdapter(Context context) {
        this.context = context;
    }

    public void setmOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    public void setData(List<SendBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public void addData(int position) {
        notifyItemInserted(position);
    }



    @NonNull
    @Override
    public SuperViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        SuperViewHolder viewHolder = SuperViewHolder.createViewHolder(context, viewGroup, R.layout.item_send);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull SuperViewHolder holder, final int position) {
        final SendBean sendBean = list.get(position);

        EditText etAddress = holder.getView(R.id.tv_addr);
        EditText etBtc = holder.getView(R.id.tv_btc);
        final TextView tv_low = holder.getView(R.id.tv_low);
        final TextView tv_mid = holder.getView(R.id.tv_mid);
        final TextView tv_hig = holder.getView(R.id.tv_hig);
        if(sendBean.getSat() != 0){
            switch (sendBean.getSat()){
                case 12:
                    tv_low.setBackgroundColor(context.getResources().getColor(R.color.blue_e4));
                    tv_mid.setBackgroundColor(context.getResources().getColor(R.color.white));
                    tv_hig.setBackgroundColor(context.getResources().getColor(R.color.white));
                    tv_low.setTextColor(context.getResources().getColor(R.color.white));
                    tv_mid.setTextColor(context.getResources().getColor(R.color.black_3));
                    tv_hig.setTextColor(context.getResources().getColor(R.color.black_3));
                    break;
                case 24:
                    tv_low.setBackgroundColor(context.getResources().getColor(R.color.white));
                    tv_mid.setBackgroundColor(context.getResources().getColor(R.color.blue_e4));
                    tv_hig.setBackgroundColor(context.getResources().getColor(R.color.white));
                    tv_low.setTextColor(context.getResources().getColor(R.color.black_3));
                    tv_mid.setTextColor(context.getResources().getColor(R.color.white));
                    tv_hig.setTextColor(context.getResources().getColor(R.color.black_3));
                    break;
                case 48:
                    tv_low.setBackgroundColor(context.getResources().getColor(R.color.white));
                    tv_mid.setBackgroundColor(context.getResources().getColor(R.color.white));
                    tv_hig.setBackgroundColor(context.getResources().getColor(R.color.blue_e4));
                    tv_low.setTextColor(context.getResources().getColor(R.color.black_3));
                    tv_mid.setTextColor(context.getResources().getColor(R.color.black_3));
                    tv_hig.setTextColor(context.getResources().getColor(R.color.white));
                    break;

            }
        }


        tv_low.setOnClickListener(v -> {
            sendBean.setSat(12);
            tv_low.setBackgroundColor(context.getResources().getColor(R.color.blue_e4));
            tv_mid.setBackgroundColor(context.getResources().getColor(R.color.white));
            tv_hig.setBackgroundColor(context.getResources().getColor(R.color.white));
            tv_low.setTextColor(context.getResources().getColor(R.color.white));
            tv_mid.setTextColor(context.getResources().getColor(R.color.black_3));
            tv_hig.setTextColor(context.getResources().getColor(R.color.black_3));
        });
        tv_mid.setOnClickListener(v -> {
            sendBean.setSat(24);
            tv_low.setBackgroundColor(context.getResources().getColor(R.color.white));
            tv_mid.setBackgroundColor(context.getResources().getColor(R.color.blue_e4));
            tv_hig.setBackgroundColor(context.getResources().getColor(R.color.white));
            tv_low.setTextColor(context.getResources().getColor(R.color.black_3));
            tv_mid.setTextColor(context.getResources().getColor(R.color.white));
            tv_hig.setTextColor(context.getResources().getColor(R.color.black_3));
        });
        tv_hig.setOnClickListener(v -> {
            sendBean.setSat(48);
            tv_low.setBackgroundColor(context.getResources().getColor(R.color.white));
            tv_mid.setBackgroundColor(context.getResources().getColor(R.color.white));
            tv_hig.setBackgroundColor(context.getResources().getColor(R.color.blue_e4));
            tv_low.setTextColor(context.getResources().getColor(R.color.black_3));
            tv_mid.setTextColor(context.getResources().getColor(R.color.black_3));
            tv_hig.setTextColor(context.getResources().getColor(R.color.white));
        });
        tv_low.setTag(position);
        if(tv_low.getTag()==null){
            tv_low.setBackgroundColor(context.getResources().getColor(R.color.white));
            tv_mid.setBackgroundColor(context.getResources().getColor(R.color.blue_e4));
            tv_hig.setBackgroundColor(context.getResources().getColor(R.color.white));
            tv_low.setTextColor(context.getResources().getColor(R.color.black_3));
            tv_mid.setTextColor(context.getResources().getColor(R.color.white));
            tv_hig.setTextColor(context.getResources().getColor(R.color.black_3));
        }
        if (etAddress.getTag() != null) {
            etAddress.removeTextChangedListener((TextWatcher) etAddress.getTag());
        }

        AddTypeNameWatcher addressWatcher = new AddTypeNameWatcher(sendBean,0);
        etAddress.addTextChangedListener(addressWatcher);
        etAddress.setTag(addressWatcher);
        etAddress.setText(sendBean.getAddress());

        if (etBtc.getTag() != null) {
            etBtc.removeTextChangedListener((TextWatcher) etBtc.getTag());
        }

        AddTypeNameWatcher btcWatcher = new AddTypeNameWatcher(sendBean,1);
        etBtc.addTextChangedListener(btcWatcher);
        etBtc.setTag(btcWatcher);
        etBtc.setText(sendBean.getNum());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
}
